package com.tjlg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjlg.auto.JSONAuthentication;
import com.tjlg.entity.WaUser;
import com.tjlg.mapper.WaUserMapper;
import com.tjlg.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tjlg
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/wa-user")
public class WaUserController extends JSONAuthentication {
    @Autowired
    WaUserMapper waUserMapper;
    @ApiOperation(value = "添加用户")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("username") String username, @RequestParam("password") String password1, @RequestParam("role") String role) throws  Exception{
        WaUser anUser=new WaUser();
        anUser.setUsername(username);
        anUser.setPassword(password1);
        anUser.setRole(role);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("username",anUser.getUsername());
        WaUser a= waUserMapper.selectOne(wrapper);
        if(a!=null){
            Result result= Result.error();
            result.setMessage("该用户已存在");
            this.WriteJSON(request, response, result);
        }else {
            String password= new BCryptPasswordEncoder().encode(anUser.getPassword());
            anUser.setPassword(password);
            int i=waUserMapper.insert(anUser);
            if(i>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除用户")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=waUserMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有用户")
    @PostMapping("/findAllUser")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<WaUser> shUserPage=new Page<>(current,size);
        Page<WaUser> shUserPages=waUserMapper.selectPage(shUserPage,null);
        List<WaUser> records = shUserPages.getRecords();
        int total=waUserMapper.selectCount(null);

        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

